<template>
  <basic-container>
    <avue-crud
      ref="crud"
      :data="data"
      :option="option"
      :page.sync="page"
      :before-open="beforeOpen"
      :cell-style="cellStyle"
      @on-load="rowLoad"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
    ></avue-crud>
  </basic-container>
</template>

<script>
import avue from "@/mixins/avue.js";
import { SE, iconList } from "@/common/constants.js";
import {
  menu_tree,
  menu_edit,
  menu_add,
  menu_delete,
} from "@/api/core/permission";

export default {
  mixins: [avue],
  data() {
    return {
      data: [],
      option: {
        dialogDirection: "rtl",
        dialogType: "drawer",
        column: [
          {
            label: "菜单名称",
            prop: "name",
            align: "left",
            rules: [
              {
                required: true,
                message: "必填",
                trigger: "blur",
              },
            ],
          },
          {
            label: "类型",
            prop: "type",
            type: "radio",
            dicData: [
              {
                label: "功能",
                value: 0,
              },
              {
                label: "菜单",
                value: 1,
              },
            ],
            value: 1,
          },
          {
            label: "上级菜单",
            prop: "parentId",
            type: "tree",
            props: {
              label: "name",
              value: "id",
            },
            dicdata: [],
            // propsHttp: {
            //   home: baseUrl2,
            //   res: "data",
            //   url: "path",
            // },
            // dicFormatter: (res) => {
            //   return res.data; //返回字典的层级结构
            // },
            hide: true,
          },
          {
            label: "访问路径",
            prop: "url",
            align: "left",
            // hide: true
          },
          {
            label: "组件目录",
            prop: "component",
            align: "left",
            // hide: true
          },
          {
            label: "图标选择",
            prop: "icon",
            type: "icon",
            value: "el-icon-s-operation",
            iconList: iconList,
          },
          {
            label: "状态",
            prop: "status",
            type: "radio",
            dicData: SE,
            value: "1",
          },
          {
            label: "排序",
            prop: "sort",
            type: "number",
            value: 1,
          },
        ],
      },
    };
  },
  methods: {
    cellStyle({ row, column }) {
      if (column.property == "status") {
        if (row.status === "1") {
          return {
            color: "green",
            fontWeight: "bold",
            fontSize: "20",
          };
        } else if (row.status === "0") {
          return {
            color: "red",
            fontWeight: "bold",
            fontSize: "20",
          };
        }
      }
    },
    beforeOpen(done, type) {
      if (["edit", "add"].includes(type)) {
        var crud = this.$refs.crud;
        menu_tree({ type: 1 }).then((res) => {
          crud.updateDic("parentId", this.$s.res(res));
        });
      }
      done();
    },
    rowLoad() {
      menu_tree().then((res) => {
        this.data = this.$s.res(res);
      });
    },
    rowSave(row, done, loading) {
      menu_add(row)
        .then((res) => {
          this.$s.suc(res);
          loading();
          this.rowLoad();
          done();
        })
        .catch((err) => {
          console.log(err);
          loading();
        });
    },
    rowUpdate(row, index, done, loading) {
      menu_edit(row)
        .then((res) => {
          this.$s.suc(res);
          loading();
          this.rowLoad();
          done();
        })
        .catch(() => {
          loading();
        });
    },
    rowDel(row, index, done) {
      this.$confirm("确定数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        menu_delete({ id: row.id }).then((res) => {
          this.$s.suc(res);
          this.rowLoad();
          done();
        });
      });
    },
  },
  mounted() {},
};
</script>

<style></style>
